package com.liao.jta.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import javax.sql.XADataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.test")
    public XADataSource initDataSource(){
        return new MysqlXADataSource();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public XADataSource initDataSource1(){
        return new MysqlXADataSource();
    }

    @Bean
    @Primary
    public DataSource testDataSource(){
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(initDataSource());
        atomikosDataSourceBean.setUniqueResourceName("jta_test");
        atomikosDataSourceBean.setPoolSize(5);
        return atomikosDataSourceBean;
    }

    @Bean
    public DataSource test1DataSource(){
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("jta_test1");
        atomikosDataSourceBean.setXaDataSource(initDataSource1());
        return atomikosDataSourceBean;
    }
}
